﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Soft
{

    /// <summary>
    /// 将结果序列化为Json
    /// </summary>
    [Serializable]
    public class ResultJson : Result
    {

        /// <summary>
        /// 数据类型
        /// </summary>
        public string DataType { get; set; }

        
        /// <summary>
        /// 是否为空
        /// </summary>
        public bool IsVoid { get; set; } = true;


        /// <summary>
        /// 数据
        /// </summary>
        public string Json { get; set; }


        public ResultJson()
        {
        }


        public ResultJson(IResult result) : base(result)
        {
        }


        public ResultJson(string error) : base(error, null)
        {
        }


        public ResultJson(Exception e) : base(e, null)
        {
        }


        /// <summary>
        /// 设置functionCode 没有权限
        /// </summary>
        /// <param name="functionCode"></param>
        public override void SetNotAuthorized(string functionCode)
        {
            base.SetNotAuthorized(functionCode);
            this.Json = JsonConvertHelper.JsonNull;
        }


        /// <summary>
        /// 将ResultJson反序列化为对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T Convert<T>()
        {
            if (string.IsNullOrWhiteSpace(this.Json))
            {
                return default(T);
            }
            return JsonConvertHelper.FormJson<T>(this.Json);
        }


        /// <summary>
        /// 将string 的值转换为ResultJson
        /// </summary>
        /// <param name="value"></param>
        public static explicit operator ResultJson(string value)
        {
            return new ResultJson
            {
                Json = value
            };
        }


        /// <summary>
        /// 将ResultJson转化为ResultJson
        /// </summary>
        /// <param name="value"></param>
        public static explicit operator string(ResultJson value)
        {
            if (value != null)
            {
                return value.Json;
            }
            return null;
        }


    }
}
